package com.my.study.designPatterns.observerPattern.basic;

import com.my.study.designPatterns.observerPattern.basic.Observer;
import com.my.study.designPatterns.observerPattern.basic.Subject;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体的主题类
 */
public class WeatherData implements Subject {


    private List<Observer> observers = new ArrayList<>();

    private float temperature;

    private float humidity;

    private float pressure;

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {

        int i = observers.indexOf(observer);
        if(i >= 0){
            observers.remove(i);
        }
    }

    @Override
    public void notifyObservers() {

        for(Observer observer : observers){

            observer.update(temperature, humidity, pressure);
        }
    }

    public void meassurementsChanged(){
        notifyObservers();
    }

    public void setMeasurements(float temperature, float humidity, float pressure){
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        meassurementsChanged();
    }
}
